package io.kiki.sba.registry.server.metadata.slot.balance;

import io.kiki.sba.registry.util.MathUtils;
import lombok.Setter;


@Setter
public class BalancePolicyImpl implements BalancePolicy {

    public static final String PROP_LEADER_MAX_MOVE = "registry.slot.leader.max.move";
    public static final String PROP_FOLLOWER_MAX_MOVE = "registry.slot.follower.max.move";
    public static final String PROP_BALANCE_THRESHOLD = "registry.slot.balance.threshold";

    public static final int DEF_LEADER_MAX_MOVE = 6;

    private int balanceThreshold = Integer.getInteger(PROP_BALANCE_THRESHOLD, 10);

    private int maxMoveLeaderSlots = Integer.getInteger(PROP_LEADER_MAX_MOVE, DEF_LEADER_MAX_MOVE);

    private int maxMoveFollowerSlots = Integer.getInteger(PROP_FOLLOWER_MAX_MOVE, 10);

    @Override
    public int getLowWaterMarkSlotFollowerNums(int average) {
        // same as getLowWaterMarkSlotLeaderNums
        return getLowWaterMarkSlotLeaderNums(average);
    }

    @Override
    public int getLowWaterMarkSlotLeaderNums(int average) {
        // round down
        return average * (100 - balanceThreshold) / 100;
    }

    @Override
    public int getHighWaterMarkSlotFollowerNums(int average) {
        // same as getHighWaterMarkSlotLeaderNums
        return getHighWaterMarkSlotLeaderNums(average);
    }

    @Override
    public int getHighWaterMarkSlotLeaderNums(int average) {
        // round up
        return MathUtils.divideCeil(average * (100 + balanceThreshold), 100);
    }

    @Override
    public int getMaxMoveLeaderSlots() {
        return maxMoveLeaderSlots;
    }

    @Override
    public int getMaxMoveFollowerSlots() {
        return maxMoveFollowerSlots;
    }

}
